//
//  HomeVideoView.m
//  Rail
//
//  Created by hychen on 2018/7/15.
//  Copyright © 2018年 Hychen. All rights reserved.
//

#import "HomeVideoView.h"
#import "HomeVideoCollectionViewCell.h"
#import "AppDelegate.h"

@interface HomeVideoView ()
@property (nonatomic, strong) NSArray *dataArray;
@end

@implementation HomeVideoView

+ (CGFloat)heightForVideoViewWithDataArray:(NSArray *)dataArray {
    if ([dataArray isKindOfClass:[NSArray class]] && dataArray.count > 0) {
        return 125 + (SCREEN_WIDTH-30-20)/3;
    }
    return 0;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupSubviews];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    CGFloat height = self.frame.size.height;
    self.headerView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 50);
    self.lineView.frame = CGRectMake(0, height-10, SCREEN_WIDTH, 10);
    self.collectionView.frame = CGRectMake(0, 50, SCREEN_WIDTH, height-60);
}

- (void)setupSubviews {
    
    HomeTableSectionView *topView = [HomeTableSectionView sectionView];
    topView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 50);
    self.headerView = topView;
    topView.titleLabel.text = @"视频记录";
    topView.rightLabel.text = @"更多";
    [topView setBlockClickRight:^{
        BaseNavigationController *nav = (BaseNavigationController *)[((AppDelegate *)AppDelegate_bb).tabBarController.viewControllers objectAtIndexSafe:2];
        FavCollectionViewController *vc = nav.viewControllers.firstObject;
        if ([vc isKindOfClass:[FavCollectionViewController class]]) {
            [vc setSegmentIndex:1];
        }
        [((AppDelegate *)AppDelegate_bb).tabBarController setSelectedIndex:2];
    }];
    [self addSubview:topView];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 15;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.sectionInset = UIEdgeInsetsMake(0, 15, 0, 15);
    CGFloat height = 125 + (SCREEN_WIDTH-30-20)/3 - 60;
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 50, SCREEN_WIDTH, height) collectionViewLayout:layout];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.backgroundColor = [UIColor clearColor];
    self.collectionView = collectionView;
    [self addSubview:collectionView];
    [self.collectionView registerNib:[UINib nibWithNibName:@"HomeVideoCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"HomeVideoCollectionViewCell"];
    
    BMLineView *lineView = [BMLineView lineViewWithFrame:CGRectMake(0, 220, SCREEN_WIDTH, 10) color:RGB(238, 244, 252)];
    self.lineView = lineView;
    [self addSubview:lineView];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HomeVideoHistoryModel *model = [self.dataArray objectAtIndexSafe:indexPath.row];
    if ([model isKindOfClass:[HomeVideoHistoryModel class]]) {
        HomeVideoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeVideoCollectionViewCell" forIndexPath:indexPath];
        cell.model = model;
        return cell;
    }
    return [UICollectionViewCell new];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    HomeVideoHistoryModel *model = [self.dataArray objectAtIndexSafe:indexPath.row];
    if ([model isKindOfClass:[HomeVideoHistoryModel class]]) {
        return CGSizeMake((SCREEN_WIDTH-30-20)/3, 175);
    }
    return CGSizeZero;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    HomeVideoHistoryModel *model = [self.dataArray objectAtIndexSafe:indexPath.row];
    if ([model isKindOfClass:[HomeVideoHistoryModel class]]) {
        if (self.BlockClickItem) {
            self.BlockClickItem(model);
        }
    }
    else {
        NSAssert(1==2, @"HomeVideoHistoryModel出错");
    }
}

- (void)reloadWithDataArray:(NSArray *)dataArray {
    _dataArray = dataArray;
    [self.collectionView reloadData];
}

- (NSArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSArray array];
    }
    return _dataArray;
}
@end
